import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Run {
    public static final int runner = 5;
    public CountDownLatch begin;
    public CountDownLatch end;

    public CountDownLatch getBegin() {
        return begin;
    }

    public void setBegin(CountDownLatch begin) {
        this.begin = begin;
    }

    public CountDownLatch getEnd() {
        return end;
    }

    public void setEnd(CountDownLatch end) {
        this.end = end;
    }


    public Run(){
        begin = new CountDownLatch(1);
        end = new CountDownLatch(runner);
    }

    public static void main(String[] args){
        Run run = new Run();
        CountDownLatch begin = run.getBegin();
        CountDownLatch end = run.getEnd();
        Runner[] runners = new Runner[runner];
        ExecutorService executorService = Executors.newFixedThreadPool(runner);

        for (int i = 0; i < runner; i++) {
            runners[i] = new Runner(begin,end,"NO:" + (i + 1));
            executorService.submit(runners[i]);
        }
        begin.countDown();

        try {
            end.await();
            System.out.println("比赛结束，所有运动员完成比赛");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Runner implements Runnable{

    public CountDownLatch begin;
    public CountDownLatch end;
    public String runner_NO;

    public Runner(CountDownLatch begin, CountDownLatch end, String runner_NO) {
        this.begin = begin;
        this.end = end;
        this.runner_NO = runner_NO;
    }

    @Override
    public void run() {

        try {
            begin.await();
            long time = new Random().nextInt(10000);
            System.out.println("运动员" + runner_NO + "耗时" + time + "秒完成比赛");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            end.countDown();
        }
    }
}